package com.extension.company.model.home;

import android.util.Log;

import com.extension.company.Constant;
import com.extension.company.common.http.RetrofitHelper;
import com.extension.company.contract.home.GoodInfoFragmentContract;
import com.extension.company.eventbus.DetailComment;
import com.extension.company.model.api.RetrofitUrlApi;
import com.extension.company.model.entity.AddMyShopCart;
import com.extension.company.model.entity.GoodsInfoDetail2;
import com.extension.company.model.entity.GoodsInforBean;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：zwk
 * 时间：on 2019/2/19 13:53
 * 功能：商品详情页的网络请求
 */
public class GoodsInfoFragmentMode implements GoodInfoFragmentContract.Mode {

	private RetrofitUrlApi retrofitUrlApi = RetrofitHelper.getRetrofit().create(RetrofitUrlApi.class);

	/**
	 * @param goodsId 商品goodsId
	 */
	@Override
	public void goodsAndCommitRequest(final int goodsId, final GoodInfoFragmentContract.M2P m2P) {
		retrofitUrlApi.GoodsAndCommit(Constant.SIGN, goodsId) // Constant.TOKEN,
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<GoodsInfoDetail2>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(GoodsInfoDetail2 goodsInforBean) {
//						if (goodsInforBean.getCode() == 200) {
////							m2P.m2pSuccess(goodsInforBean);
//							List<GoodsInforBean.Data> commentList=goodsInforBean.getData().getGoods_comment();
//  						EventBus.getDefault().post();
//						}
						m2P.m2p(goodsInforBean);

						if (goodsInforBean.getCode() == 200) { // 将评论信息传给评论界面
							if (null != goodsInforBean.getData().getGoods_comment()) {
								List<GoodsInfoDetail2.DataBean.GoodsCommentBean> commentList = goodsInforBean.getData().getGoods_comment();
								EventBus.getDefault().post(commentList);
							}
						}
					}

					@Override
					public void onError(Throwable e) {
						Log.d("zwk", "请求报错" + e);
						m2P.m2pFaile();
					}

					@Override
					public void onComplete() {

					}
				});

	}

	/**
	 * 添加到购物车
	 *
	 * @param goodId
	 * @param goodsNum
	 * @param goodSkuId
	 * @param callback
	 */
	@Override
	public void addCart(String goodId, String goodsNum, String goodSkuId, final GoodInfoFragmentContract.addM2P callback) { // 点击加入购物车方法
		retrofitUrlApi.addCart(Constant.SIGN, Constant.TOKEN, goodId, goodsNum, goodSkuId)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<AddMyShopCart>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(AddMyShopCart addMyShopCart) {
						if (addMyShopCart.getCode() == 200) {
							callback.addGoods2Cart(addMyShopCart.getMsg());
						}
					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});
	}
}
